package menu;

import java.util.ArrayList;
import java.util.List;

public class TextMenu implements Menu {
    private final String aPrompt;
    private final String aTitle;
    private final List<MenuElement> aElementList;
    
    public TextMenu(String pPrompt, String pTitle) {
        assert pPrompt != null && pTitle != null;
        
        aPrompt = pPrompt;
        aTitle = pTitle;
        aElementList = new ArrayList<MenuElement>();
    }

    public void addElement(MenuElement pElement) {
        assert pElement != null;
        aElementList.add(pElement);
        
    }

    public MenuElement getElement(int pIndex) {
        assert pIndex >= 0 && pIndex < aElementList.size();
        return aElementList.get(pIndex);
    }

    public int getLength() {
        return aElementList.size();
    }
    
    public String getTitle() {
        return aTitle;
    }
    
    public String getPrompt() {
        return aPrompt;
    }

    // Returns null if no MenuElement was found
    // which uses the given hotkey.
    public MenuElement getElement(char pHotkey) {
        for(MenuElement lElement : aElementList) {
            if(lElement.matches(pHotkey)) {
                return lElement;
            }
        }
        return null;
    }
    
    // Returns true if one of the menu elements in this menu
    // has the given hotkey.
    public boolean contains(char pHotkey) {
        return getElement(pHotkey) != null;
    }
}
